<?php

class Default_AuthenticationController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        //$this->_form = new Default_Form_Login();
        
    }

    public function indexAction()
    {
        /*if(Zend_Auth::getInstance()->hasIdentity()){
            $auth = Zend_Auth::getInstance();
            $name = $auth->getIdentity()->user_fullname;
            $this->view->name = $name;
        }
        else
            $this->view->form_login = $this->_form;*/
    }

    public function loginAction()
    {
        
        if(!Zend_Auth::getInstance()->hasIdentity()){
            //if user hasn't logged they must login
            $form = new Default_Form_Login();
            $this->view->form_login = $form;
            $this->view->status = false;
            
            if($this->_request->isPost()){
                if($form->isValid($this->_request->getPost())){//check valid of user fill in form
                    $username = $form->getValue("username");//
                    $password = $form->getValue("password");//
                    $remember = $form->getValue("remember");//get value from form login
            
                    $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
                    $authAdapter->setTableName("user")//set table want to use to authenticate
                    ->setIdentityColumn("user_email")///
                    ->setCredentialColumn("password");///set colunm require
                    $authAdapter->setIdentity($username)
                                ->setCredential(md5($password));
                    $authAdapter->getDbSelect()->where('user_actived = ?', 1);
                    
                    $auth = Zend_Auth::getInstance();
                    $result = $auth->authenticate($authAdapter);
                    if($result->isValid()){
                        $identity = $authAdapter->getResultRowObject(null, array('password'));
                        $auth->getStorage()->write($identity);
                        if($remember == 1){
                            Zend_Session::rememberMe(86400*2);
                        }
                        $name = $auth->getIdentity()->user_fullname;
                        $role = $auth->getIdentity()->user_role;
                        
                        $this->view->name = $name;
                        $this->view->status = true;
                        $this->view->role = $role;
                       
                        $this->view->userid = $auth->getIdentity()->user_id;
                    }
                    else{
                        $form->password->clearErrorMessages();
                        $form->password->addError('Sai email hoặc mật khẩu');
                    }
            
                }
            }
            
            
        }
        
        else{//if user has logged then view username
            $auth = Zend_Auth::getInstance();
            $name = $auth->getIdentity()->user_fullname;
            $role = $auth->getIdentity()->user_role;
            $this->view->userid = $auth->getIdentity()->user_id;
            
            $this->view->name = $name;
            $this->view->status = true;
            $this->view->role = $role;            
        }
            
    }

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect(array(
                'module'=>'default',
                'controller'=>'index',
                'action'=>'index'
        ));
    }

    public function messageAction()
    {
        $option = array(
                'layout'=>'layout',
                'layoutPath'=>APPLICATION_PATH .'/layouts/scripts/default',      
        );
        Zend_Layout::startMvc($option);
    }


}







